package com.tourism.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tourism.dao.CustomLineDao;
import com.tourism.entity.CustomLine;
import com.tourism.entity.CustomLineQuery;
import com.tourism.exception.ExceptionEnum;
import com.tourism.exception.GlobalException;
import com.tourism.service.CustomLineService;
import com.tourism.util.SystemHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @Description
 * @Author z
 * @Date 2021-03-01 21:05
 */
@Service
public class CustomLineServiceImpl implements CustomLineService {

    @Autowired
    private CustomLineDao customLineDao;


    @Override
    public CustomLine getCustomLineById(Long id) {
        CustomLine customLine = customLineDao.getCustomLineById(id);
        return customLine;
    }

//新增定制线路
    @Override
    public boolean addCustomLine(CustomLine customLine) {
        customLine.init();
        customLine.setCustomUser(SystemHelper.getUserId());
        return customLineDao.addCustomLine(customLine);
    }

    @Override
    public boolean updateCustomLine(CustomLine customLine) {
        return customLineDao.updateCustomLine(customLine);
    }

    @Override
    public Boolean deleteCustomLineById(Long id) {
        return customLineDao.deleteCustomLineById(id);
    }

    @Override
    public boolean updateCustomLineAccept(Long id, Boolean accept) throws GlobalException {
        if (!SystemHelper.isAdmin()) {
            throw new GlobalException(ExceptionEnum.NotOperate);
        }
        return customLineDao.updateCustomLineAccept(id, accept);

    }


    @Override
    public PageInfo<CustomLine> getCustomLineList(CustomLineQuery customLineQuery) {
        PageHelper.startPage(customLineQuery.getPageNum(), customLineQuery.getPageSize());
        //判断是否是管理员（管理员查询所有，否之用户只能查询自己的）
        if (!SystemHelper.isAdmin()) {
            customLineQuery.setCustomUser(SystemHelper.getUserId());
        }
        List<CustomLine> CustomLineList = customLineDao.getCustomLineList(customLineQuery);
        PageInfo<CustomLine> result = new PageInfo<>(CustomLineList);
        return result;
    }


}
